package com.fifth.apartment.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.apartment.entity.*;
import com.fifth.apartment.result.Result;
import com.fifth.apartment.service.EquipmentService;
import com.fifth.apartment.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/admin/equipment")
@Api(tags = "设备管理接口")
@Slf4j
public class EquipmentController {

    @Autowired
    private EquipmentService service;

    @GetMapping("findPage")
    @ApiOperation("设备分页查询")
    public Result findPage(PageQueryVO vo){

        try {
            IPage<Equipment> page  = service.findPage(vo);
            System.out.println();
            return Result.success(page);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("fingType")
    @ApiOperation("设备类型查询")
    public Result findType(){
        try {
            EquipmentType[] types = service.findType();
            if (types!=null)
            return new Result(true,"查询成功",types);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
       return new Result(false,"查询不成功");
    }


    @GetMapping("findBuildingType")
    @ApiOperation("楼栋查询")
    public Result findBuildingType(){
        try {
            BuildingInfo[] types = service.findBuildingType();
            if (types!=null){
                return new Result(true,"查询成功",types);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new Result(false,"查询不成功");
    }

    @GetMapping("getEquipmentById")
    @ApiOperation("按照id查询设备信息")
    public Equipment getEquipmentById(Integer id){
        Equipment equipment = service.getEquipmentById(id);
        return equipment;
    }

    @PutMapping("putDetailsById")
    @ApiOperation("根据id修改设备是否报修")
    public Result putDetailsById(Integer id, String details, String state){
        Boolean flag = service.putDetailsById(id,details,state);
        if (flag){
            return new Result(true,"更新成功");
        }
        return new Result(false,"更新失败");
    }

    @DeleteMapping("deleteEquipment")
    @ApiOperation("根据id删除设备")
    public Result deleteEquipment(Integer id){
        Integer flag = service.deleteEquipment(id);

        return flag>0?new Result(true,"删除成功"):new Result(false,"删除失败");
    }
}
